import time

import sqlalchemy as sa
from sqlalchemy import UniqueConstraint

from app import Base

Coin = sa.Table(
    # 币名字  zh en
    # 缩写
    # icon
    # 排行 （交易总额）
    # 当前价格 美元 人民币
    # 市值
    # 24 成交量 额
    # 最低
    # 最高
    "currency",
    Base,
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('slug', sa.String(128), unique=True, doc="缩写名"),
    sa.Column('abbr', sa.String(128), unique=True),
    sa.Column('name_zh', sa.String(128), unique=True),
    sa.Column('name_en', sa.String(128), unique=True),
    sa.Column('icon_url', sa.String(128)),
    sa.Column('website_url', sa.String(128)),
    sa.Column('website_url_name', sa.String(128)),
    sa.Column('change', sa.Integer),
    sa.Column('price_usd', sa.DECIMAL(30, 16), doc="现价"),
    sa.Column('price_cny', sa.DECIMAL(30, 16)),
    sa.Column('market_cap_usd', sa.DECIMAL(30, 16), doc="市场流通量"),
    sa.Column('market_cap_cny', sa.DECIMAL(30, 16)),
    sa.Column('vol', sa.DECIMAL(30, 8), doc="市场交易量"),
    sa.Column('volume_usd', sa.DECIMAL(30, 16), doc="市场流通量美元价值"),
    sa.Column('volume_cny', sa.DECIMAL(30, 16)),
    sa.Column('created_at', sa.Integer, default=time.time()),
    sa.Column('updated_at', sa.Integer, default=time.time()),
    sa.Column('is_stable', sa.BOOLEAN, doc="展示开关"),
    sa.Column('enabled', sa.Boolean, doc="上架开关"),
    sa.Column('alive', sa.BOOLEAN),
    sa.Column('search_tag', sa.VARCHAR(256)),
    sa.Column('kline_enabled', sa.BOOLEAN),
)

Exchange = sa.Table(
    # 交易所名字 中文 英文
    # 交易所code  （英文名）
    # 交易所 官网地址
    # 币总数 交易对总数
    # 图标
    # 交易总额
    'exchanges',
    Base,
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('code', sa.String(32), unique=True),
    sa.Column('name', sa.String(128), index=True),
    sa.Column('description', sa.String(512)),
    sa.Column('url', sa.String(128)),
    sa.Column('icon', sa.String(128)),
    sa.Column('enabled', sa.BOOLEAN),
    sa.Column('created_at', sa.Integer, default=time.time()),
    sa.Column('updated_at', sa.Integer, default=time.time()),
)

ExchangeCurrencyPairs = sa.Table(
    # 交易对现价表
    # 前、后   币id 缩写
    # 24 成交量 额
    # 交易所 id
    # 是否显示 （开关）
    # 交易对名字
    # 开高低手
    # 涨跌幅

    'exchange_currency_pairs',
    Base,
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('exchange_id', sa.Integer),
    sa.Column('exchange_code', sa.String(32), ),
    sa.Column('exchange_name', sa.String(128)),
    sa.Column('pair_symbol', sa.String(32)),
    sa.Column('ticker_enabled', sa.Boolean),
    sa.Column('kline_enabled', sa.Boolean),
    sa.Column('base_currency_id', sa.Integer),
    sa.Column('base_currency_slug', sa.String(128)),
    sa.Column('quote_currency_id', sa.Integer),
    sa.Column('quote_currency_slug', sa.String(128)),
    sa.Column('show_pair_symbol', sa.String(64)),
    sa.Column('open', sa.DECIMAL(30, 12)),
    sa.Column('high', sa.DECIMAL(30, 12)),
    sa.Column('low', sa.DECIMAL(30, 12)),
    sa.Column('close', sa.DECIMAL(30, 12)),
    sa.Column('close_usd', sa.DECIMAL(30, 12)),
    sa.Column('close_cny', sa.DECIMAL(30, 12)),
    sa.Column('change_24h', sa.Float),
    sa.Column('base_24h_vol', sa.DECIMAL(30, 8)),
    sa.Column('quote_24h_vol', sa.DECIMAL(30, 8)),
    sa.Column('vol_24h_usd', sa.DECIMAL(30, 8)),
    sa.Column('vol_24h_cny', sa.DECIMAL(30, 8)),
    sa.Column('created_at', sa.Integer, default=time.time()),
    sa.Column('updated_at', sa.Integer, default=time.time()),
    sa.Column('ticker_updated_at', sa.Integer),
    sa.Column('exchange_icon', sa.String(128)),
    sa.Column('currency_icon', sa.String(128)),
    sa.Column('base_symbol', sa.String(32)),
    sa.Column('quote_symbol', sa.String(32)),
    sa.Column('exchange_pair_symbol', sa.String(32)),
    __table_args__=(
        UniqueConstraint('exchange_code', 'pair_symbol'),
    )
)

ExchangeCurrencyPairsHistory = sa.Table(
    # 前、后   币id 缩写
    # 交易所 id
    # 交易对名字
    # 开高低手
    # 所属时间 （一分钟一次）
    # ------------如果与K线表融合-------------
    # 涨幅
    # 成交额
    # 成交量

    'exchange_currency_pairs_history',
    Base,
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('exchange_id', sa.Integer),
    sa.Column('exchange_code', sa.String(32), ),
    sa.Column('exchange_name', sa.String(128)),
    sa.Column('pair_symbol', sa.String(32)),
    sa.Column('ticker_enabled', sa.Boolean),
    sa.Column('kline_enabled', sa.Boolean),
    sa.Column('base_currency_id', sa.Integer),
    sa.Column('base_currency_slug', sa.String(128)),
    sa.Column('quote_currency_id', sa.Integer),
    sa.Column('quote_currency_slug', sa.String(128)),
    sa.Column('show_pair_symbol', sa.String(64)),
    sa.Column('open', sa.DECIMAL(30, 12)),
    sa.Column('high', sa.DECIMAL(30, 12)),
    sa.Column('low', sa.DECIMAL(30, 12)),
    sa.Column('close', sa.DECIMAL(30, 12)),
    sa.Column('close_usd', sa.DECIMAL(30, 12)),
    sa.Column('close_cny', sa.DECIMAL(30, 12)),
    sa.Column('change_24h', sa.Float),
    sa.Column('base_24h_vol', sa.DECIMAL(30, 8)),
    sa.Column('quote_24h_vol', sa.DECIMAL(30, 8)),
    sa.Column('vol_24h_usd', sa.DECIMAL(30, 8)),
    sa.Column('vol_24h_cny', sa.DECIMAL(30, 8)),
    sa.Column('created_at', sa.Integer, default=time.time()),
    sa.Column('ticker_updated_at', sa.Integer),
    sa.Column('exchange_icon', sa.String(128)),
    sa.Column('currency_icon', sa.String(128)),
    sa.Column('base_symbol', sa.String(32)),
    sa.Column('quote_symbol', sa.String(32)),
    sa.Column('exchange_pair_symbol', sa.String(32)),
    __table_args__=(
        UniqueConstraint('exchange_code', 'pair_symbol'),
    )
)

CoinHistory24H = sa.Table(
    'exchange_currency_pairs_24_history',
    Base,
    sa.Column('id', sa.Integer, primary_key=True),

)


CoinHistoryALL = sa.Table(
    'exchange_currency_pairs_history_all',
    Base,
    sa.Column('id', sa.Integer),
    sa.Column('price_usd'),
    sa.Column('price_cny'),
    sa.Column('price_cny'),
)


ExchangeCurrencyHistory = sa.Table(
    # 币历史数据表
    # 币 id
    # 当前价格 美元 人民币
    # 价格所属时间(灭分钟一条  19456000)

    'exchange_currency_pairs_history',
    Base,
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('exchange_id', sa.Integer),
    sa.Column('exchange_code', sa.String(32), ),
    sa.Column('exchange_name', sa.String(128)),
    sa.Column('pair_symbol', sa.String(32)),
    sa.Column('ticker_enabled', sa.Boolean),
    sa.Column('kline_enabled', sa.Boolean),
    sa.Column('base_currency_id', sa.Integer),
    sa.Column('base_currency_slug', sa.String(128)),
    sa.Column('quote_currency_id', sa.Integer),
    sa.Column('quote_currency_slug', sa.String(128)),
    sa.Column('show_pair_symbol', sa.String(64)),
    sa.Column('open', sa.DECIMAL(30, 12)),
    sa.Column('high', sa.DECIMAL(30, 12)),
    sa.Column('low', sa.DECIMAL(30, 12)),
    sa.Column('close', sa.DECIMAL(30, 12)),
    sa.Column('close_usd', sa.DECIMAL(30, 12)),
    sa.Column('close_cny', sa.DECIMAL(30, 12)),
    sa.Column('change_24h', sa.Float),
    sa.Column('base_24h_vol', sa.DECIMAL(30, 8)),
    sa.Column('quote_24h_vol', sa.DECIMAL(30, 8)),
    sa.Column('vol_24h_usd', sa.DECIMAL(30, 8)),
    sa.Column('vol_24h_cny', sa.DECIMAL(30, 8)),
    sa.Column('created_at', sa.Integer, default=time.time()),
    sa.Column('ticker_updated_at', sa.Integer),
    sa.Column('exchange_icon', sa.String(128)),
    sa.Column('currency_icon', sa.String(128)),
    sa.Column('base_symbol', sa.String(32)),
    sa.Column('quote_symbol', sa.String(32)),
    sa.Column('exchange_pair_symbol', sa.String(32)),
    __table_args__=(
        UniqueConstraint('exchange_code', 'pair_symbol'),
    )
)

# K线 m15
# 同K线 m1


# 交易对关系表
# 多对多
# 两个币的 id 缩写
